package com.example.AIstudy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.AIstudy.dao.StuAndProSetDao;
import com.example.AIstudy.domain.StuAndProSet;
import com.example.AIstudy.exception.AIstudyException;
import com.example.AIstudy.service.StuAndProSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
public class StuAndProSetServiceImpl extends ServiceImpl<StuAndProSetDao, StuAndProSet> implements StuAndProSetService {

    @Autowired
    StuAndProSetDao stuAndProSetDao;

    @Override
    public void insertStuAndSet(StuAndProSet stuAndProSet) throws AIstudyException {
        try{
            //插入套题的信息，并且完成id的回插
            stuAndProSetDao.insertStuAndSet(stuAndProSet);
        }catch(Exception e){
            throw new AIstudyException(AIstudyException.ErrorType.INSERT_ERROR,"添加学生完成套题的信息失败");
        }
    }

    @Override
    @Transactional(rollbackFor = AIstudyException.class)
    public void updateStuAndSet(StuAndProSet stuAndProSet) throws AIstudyException {
        try{
            //更新套题的完成信息
            stuAndProSetDao.updateById(stuAndProSet);
        }catch(Exception e){
            throw new AIstudyException(AIstudyException.ErrorType.UPDATE_ERROR,"修改学生完成套题的信息失败");
        }
    }

    @Override
    public IPage<StuAndProSet> findStuAndSet(Map<String, Object> condition, Integer pageNum, Integer pageSize) {
        IPage page = new Page(pageNum, pageSize);
        QueryWrapper<StuAndProSet> qw = new QueryWrapper<>();
        //查询题目信息的时候可以筛选
        qw.eq(condition.containsKey("stuID"), "stuID", condition.get("stuID"));
        qw.eq(condition.containsKey("proSetID"), "proSetID", condition.get("proSetID"));
        //直接查询试题信息的时候，不显示子题信息，点进去查看详情才显示
        qw.ge(condition.containsKey("startTime"), "startTime", condition.get("startTime"));
        qw.le(condition.containsKey("endTime"), "endTime", condition.get("endTime"));
        return stuAndProSetDao.findStuAndProset(page, qw);
    }

}
